<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <%--引入高德地图--%>
    <link rel="stylesheet" href="https://cache.amap.com/lbs/main1119.css"/>
    <script type="text/javascript"
            src="https://webapi.amap.com/maps?v=1.4.9&key=b6f4478e948d415a09fb07921c241adf&plugin=AMap.Autocomplete&plugin=AMap.Geocoder"></script>
    <script type="text/javascript" src="https://cache.amap.com/lbs/addToolbar.js"></script>

    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
</head>
<body>
<div class="layui-form-item layui-form-text" style="height: 300px">
    <label class="layui-form-label">高德地图</label>
    <div class="layui-input-block">
        <%--地图大小--%>
        <div id="container" ></div>
        <div id="myPageTop">
            <table>
                <tr>
                    <td>
                        <label >按关键字搜索：</label>
                    </td>
                    <td class="column2">
                        <label >左击获取经纬度：</label>
                    </td>
                    <td>
                        <button onclick="putAddressAndOrgin()">确认</button>
                    </td>
                </tr>
                <tr>
                    <td>
                        <input type="text" id="tipinput"   autocomplete="off" onkeydown="if(event.keyCode==13)return false;" placeholder="请输入关键字进行搜索" >
                    </td>
                    <td class="column2">
                        <input type="text"  readonly="true" id="lnglat" name="lnglat" >
                    </td>
                </tr>
            </table>
        </div>
    </div>
</div>


</body>

<script type="text/javascript">

    var map = new AMap.Map("container", {
        resizeEnable: true,
        zoom: 18
    })

    //为地图注册click事件获取鼠标点击出的经纬度坐标
    var clickEventListener = wl.on('click', function(e) {

        document.getElementById("lnglat").value = e.lnglat.getLng() + ',' + e.lnglat.getLat();

        if (marker) {
            marker.setMap(null);
            marker = null;
        }
        addMarker(e.lnglat.getLng(),e.lnglat.getLat());
        //这边是数组
        var lnglatXY=[e.lnglat.getLng(),e.lnglat.getLat()];
        regeocoder(lnglatXY);
    });

    var auto = new AMap.Autocomplete({
        input: "tipinput"
    });

    //注册监听，当选中某条记录时会触发
    AMap.event.addListener(auto, "select", select);
    function select(e) {
        var lng = e.poi.location.lng;
        var lat = e.poi.location.lat;
        console.log(e.poi.location.lng);
        if (e.poi && e.poi.location) {
            wl.setZoom(15);
            wl.setCenter(e.poi.location);
            addMarker(lng,lat);
        }
    }

    var marker;
    // 实例化点标记
    function addMarker(lng,lat) {
        marker = new AMap.Marker({
            icon: "https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png",
            position: [lng, lat]
        });
        marker.setMap(wl);
    }

    //坐标-地址
    function regeocoder(lnglatXY) {  //逆地理编码
        var geocoder = new AMap.Geocoder({
            radius: 1000,
            extensions: "all"
        });
        geocoder.getAddress(lnglatXY, function(status, result) {
            if (status === 'complete' && result.info === 'OK') {
                geocoder_CallBack(result);
            }
        });
    }

    function geocoder_CallBack(data) {
        var address = data.regeocode.formattedAddress; //返回地址描述
        document.getElementById("tipinput").value=address;
    }

    function putAddressAndOrgin() {
        var address = document.getElementById("tipinput").value;
        var orgin =  document.getElementById("lnglat").value;
        parent.vue.getAddressAndOrgin(address,orgin);
    }






</script>

</html>